﻿using System;
using System.Data;
using System.Data.Common;
using QzEbook.Common;
using System.Collections.Generic;
using QzEbook.Common.Emun;
using System.Text;

/// <summary>
///ViewBooks 的摘要说明
/// </summary>
/// 

namespace QzEbook.AccessObject
{
    public class ViewBooks:abstractAccess<ViewBooks>
    {
        private List<Book> bookList;

        public List<Book> BookList
        {
            get { return bookList; }
            set { bookList = value; }
        }
        private int rows = 0;
        private int pages = 0;

        private int cId;
        private int uId;
        private int tId;
        private string keyWord;

        private string orderBy;
        private string filterAt=String.Empty;
        private int pageIndex;
        private int pageSize;

        #region 属性

        public string OrderBy
        {
            get { return orderBy; }
            set {
                orderBy = value; 
            }
        }
        public string FilterAt
        {
            get
            {
                return filterAt;
            }
            set
            {
                filterAt = value;
            }
        }
        

        public int Rows
        {
            get { return rows; }
            set { rows = value; }
        }
        public int Pages
        {
            get { return pages; }
            set { pages = value; }
        }
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }
        public int CId
        {
            get { return cId; }
            set { cId = value; }
        }
        public int UId
        {
            get { return uId; }
            set { uId = value; }
        }
        public int TId
        {
            get { return tId; }
            set { tId = value; }
        }


        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }


        public string KeyWord
        {
            get { return keyWord; }
            set { keyWord = value; }
        }

        #endregion

        public ViewBooks()
        {
        }
        

        public void selectList(ViewBookAction action)
        {
            string fs = string.Empty;

            switch(action){
                case ViewBookAction.Category :
                    fs=selectPaginationCategory();
                    break;
                case ViewBookAction.User :
                    fs=selectPaginationUser();
                    break;
                case ViewBookAction.Tag :
                    fs=selectPaginationTag();
                    break;
                case ViewBookAction.KeyWord :
                    fs=selectPaginationKeyWord();
                    break;
                default:
                    break;
            }
            SqlStringBuilder sb=new SqlStringBuilder();
            sb.appendAnd(FilterAt);
            sb.appendAnd(fs);
            FilterAt = sb.ToString();
          
            DataTable table = selectPagination();
            bookList = new List<Book>(table.Rows.Count);
            foreach (DataRow row in table.Rows)
            {
                Book book = new Book();
                book=book.bindThis(row);
                bookList.Add(book);
            }
        }

        /// <summary>
        /// 直接分页;
        /// </summary>
        /// <returns></returns>
        public DataTable selectPagination()
        {      
            return selectPagination("BID", "*", "Qz_Book", FilterAt, OrderBy, PageIndex, PageSize, ref rows, ref pages);
        }

        /// <summary>
        /// 以Tag分页;(未完成)
        /// </summary>
        /// <returns></returns>
        public string selectPaginationTag()
        {
            return string.Format("BTag LIKE '%{0}{1}{2}%'","{", TId, "}");
           
        }

        /// <summary>
        /// 以搜索关键字进行分页;
        /// </summary>
        /// <returns></returns>
        private string selectPaginationKeyWord()
        {
            Tag tag = new Tag();
            List<Tag> list=tag.SearchTag(KeyWord);


            StringBuilder sb = new StringBuilder();
            foreach (Tag item in list)
            {
                sb.AppendFormat("BTag LIKE '%{1}{0}{2}%' or ", item.Id,"{","}");
            }
            sb.AppendFormat(@"BTitle LIKE '%{0}%'",KeyWord);



            //System.Web.HttpContext.Current.Response.Write(sb.ToString());
            return sb.ToString();

        }

        /// <summary>
        /// 以类型分页;
        /// </summary>
        /// <returns></returns>
        public string selectPaginationCategory()
        {
           return string.Format("CID={0}", CId);
        }


        /// <summary>
        /// 以用户分页;
        /// </summary>
        /// <returns></returns>
        public string selectPaginationUser()
        {
            return string.Format("UID={0} ", UId);
        }

        protected override ViewBooks bindRow(DataRow row)
        {
            throw new NotImplementedException();
        }

        protected override DataTable selectTable()
        {
            throw new NotImplementedException();
        }
    }
}